package org.grow.present.controller.auth;

import org.grow.present.entity.Department;
import org.grow.present.entity.Dict;
import org.grow.present.entity.UserInfo;
import org.grow.present.entity.enums.MaterialType;
import org.grow.present.entity.enums.PurchaseBillState;
import org.grow.present.mapper.DepartmentMapper;
import org.grow.present.mapper.UserInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author: xwg
 * @CreateDate: 2024/6/5
 */

@RestController
@RequestMapping("/tool")
public class ToolController {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @GetMapping("/dicts")
    public List<Dict> getDicts() {
       List<Dict> dicts = new ArrayList<>(20);
        MaterialType[] values1 = MaterialType.values();
        List<Dict> dicts1 = Arrays.stream(values1).map(x -> {
            Dict dict = new Dict();
            dict.setLabel(x.text);
            dict.setValue(x.name());
            dict.setType("MaterialType");
            dict.setColor(x.ordinal() + "");
            return dict;
        }).collect(Collectors.toList());
        PurchaseBillState[] values2 = PurchaseBillState.values();
        List<Dict> dicts2 = Arrays.stream(values2).map(x -> {
            Dict dict = new Dict();
            dict.setLabel(x.text);
            dict.setValue(x.name());
            dict.setType("PurchaseBillState");
            dict.setColor(x.ordinal() + "");
            return dict;
        }).collect(Collectors.toList());
        dicts.addAll(dicts1);
        dicts.addAll(dicts2);
        List<UserInfo> userInfos = userInfoMapper.selectList(null);
        List<Dict> dicts3 = userInfos.stream().map(userInfo -> {
            Dict dict = new Dict();
            dict.setLabel(userInfo.getNickName());
            dict.setValue(userInfo.getUserId());
            dict.setType("UserInfo");
            dict.setColor(userInfo.getDepartmentFk());
            return dict;
        }).collect(Collectors.toList());
        dicts.addAll(dicts3);
        List<Department> departments = departmentMapper.selectList(null);
        List<Dict> dicts4 = departments.stream().map(e -> {
            Dict dict = new Dict();
            dict.setLabel(e.getDepartmentName());
            dict.setValue(e.getDepartmentId());
            dict.setType(e.getClass().getSimpleName());
            dict.setColor(e.getSupervisor());
            return dict;
        }).collect(Collectors.toList());
        dicts.addAll(dicts4);
        return dicts;
    }
}
